package coba.mailing.mail;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import coba.mailing.body.Body6;
import coba.mailing.function.NextXday;
import coba.mailing.function.SumAtractivos;
import coba.mailing.jdbc.ConnectionFactory;
import coba.mailing.model.Atractivo;
import coba.mailing.model.AtractivoSum;
import coba.mailing.model.FormularioCoordinador;
import coba.mailing.model.FormularioParticipante;
import coba.mailing.model.Guia;
import coba.mailing.model.Institucion;
import coba.mailing.model.Solicitud;
import coba.mailing.procedure.AddAllCC;
import coba.mailing.procedure.AddAllTo;
import coba.mailing.properties.MainProperties;
import coba.mailing.query.FindAtractivoByItinerarioIdQuery;
import coba.mailing.query.FindFormularioCoordinadorBySolicitudId;
import coba.mailing.query.FindFormularioParticipanteBySolicitudId;
import coba.mailing.query.FindGuiaBySolicitudId;
import coba.mailing.query.FindInstitucionById;
import coba.mailing.query.FindSolicitudWhereFechaItinerarioBetween;

public class Mail6 {

	public static void sendAll() throws ClassNotFoundException, IOException, SQLException, EmailException {

		Connection con = ConnectionFactory.getConnection();

		Calendar today0Hour = GregorianCalendar.getInstance();

		today0Hour.set(Calendar.HOUR_OF_DAY, 0);
		today0Hour.set(Calendar.MINUTE, 0);
		today0Hour.set(Calendar.SECOND, 0);
		today0Hour.set(Calendar.MILLISECOND, 0);

		Date mondayNextWeek = NextXday.nextXday(Calendar.MONDAY, today0Hour.getTime());
		Date saturdayNextWeek = NextXday.nextXday(Calendar.SATURDAY, mondayNextWeek);

		/*
		 * Solicitudes de la proxima semana
		 */
		List<Solicitud> listSolicitud = FindSolicitudWhereFechaItinerarioBetween.find(con, mondayNextWeek, saturdayNextWeek);

		for (Solicitud solicitud : listSolicitud) {

			List<Atractivo> listAtractivo = FindAtractivoByItinerarioIdQuery.find(con, solicitud.getItinerarioId());
			List<AtractivoSum> listAtractivoSum = SumAtractivos.sumAtractivos(listAtractivo);

			if (listAtractivoSum.size() == 0)
				continue;

			Institucion institucion = FindInstitucionById.find(con, solicitud.getInstitucionId());

			List<FormularioCoordinador> listFormularioCoordinador = FindFormularioCoordinadorBySolicitudId.find(con, solicitud.getSolicitudId());
			List<FormularioParticipante> listFormularioParticipante = FindFormularioParticipanteBySolicitudId.find(con, solicitud.getSolicitudId());

			List<Guia> listGuia = FindGuiaBySolicitudId.find(con, solicitud.getSolicitudId());

			if (solicitud.getContactoEmail() != null && !solicitud.getContactoEmail().trim().equals("")) {
				send(solicitud, listAtractivoSum, institucion, listFormularioCoordinador, listFormularioParticipante, listGuia);
			}
		}

		//send(mondayNextWeek, saturdayNextWeek, setDias);

		con.close();
	}

	public static void send(Solicitud solicitud, List<AtractivoSum> listAtractivoSum, Institucion institucion, List<FormularioCoordinador> listFormularioCoordinador, List<FormularioParticipante> listFormularioParticipante, List<Guia> listGuia) throws EmailException, IOException {
		MainProperties mainProperties = MainProperties.getInstance();

		EmailAttachment attachment = new EmailAttachment();

		attachment.setURL(ClassLoader.getSystemClassLoader().getResource("Anexo-Transporte.pdf"));
		attachment.setDisposition(EmailAttachment.ATTACHMENT);
		attachment.setDescription("Anexo Transporte");
		attachment.setName("Anexo-Transporte.pdf");

		HtmlEmail email = new HtmlEmail();

		email.setCharset("utf-8");
		email.setHostName(mainProperties.getProperty("smtpServer"));
		email.setSmtpPort(Integer.valueOf(mainProperties.getProperty("smtpPort")));

		email.setFrom(mainProperties.getProperty("fromMail"), mainProperties.getProperty("fromName"));

		AddAllTo.addAllTo(email, solicitud.getContactoEmail());
		AddAllCC.addAllCC(email, mainProperties.getProperty("mail6Ccs"));

		email.setSubject("Conociendo BA - Visita " +
				new SimpleDateFormat("dd/MM/yyyy").format(solicitud.getFechaItinerario()));

		String cid = email.embed(ClassLoader.getSystemClassLoader().getResource("coba-mail-footer.jpg"), "Conociendo footer");

		email.setHtmlMsg(Body6.get(cid, solicitud, listAtractivoSum, institucion, listFormularioCoordinador, listFormularioParticipante, listGuia));

		email.attach(attachment);

		email.send();
	}

}
